﻿@using MusicX.Web.Shared.Songs
<div class="font-weight-bold text-center align-middle">
    @if (this.MediaPlayer.CurrentSong == null)
    {
        @:Please play a song.
    }
    else
    {
        @this.MediaPlayer.CurrentSong.Title
    }
</div>
<audio id="playerElement" preload="none" controls style="max-width:100%;">
</audio>

@code {
    protected override async Task OnInitializedAsync()
    {
        this.MediaPlayer.OnChange += this.StateHasChanged;
        var songIds = await this.JsRuntime.StorageRead("NowPlayingSongs");
        if (songIds != null)
        {
            var response = await this.ApiClient.GetSongsByIds(new GetSongsByIdsRequest { SongIds = System.Text.Json.JsonSerializer.Deserialize<IEnumerable<int>>(songIds) });
            if (response?.Data?.Songs != null)
            {
                foreach (var songListItem in response?.Data?.Songs)
                {
                    this.MediaPlayer.Add(songListItem);
                }
            }
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        this.MediaPlayer.Initialize();
    }
}
